package com.nuanshui.heatedloan.credit.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.pay.AllinPayResultNotify;
import com.nuanshui.heatedloan.credit.pay.AllinPayResultCode;
import com.nuanshui.heatedloan.credit.pay.AllinPayTool;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zss
 * @date 2017/7/28 10:34
 * @description 代付款订单状态处理
 * @Since
 */
@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_DEAL_TRADE_ORDER)
public class DealTradeOrderHandler implements RabbitmqMessageHandler {

    private static Logger logger = LoggerFactory.getLogger(DealTradeOrderHandler.class);

    @Autowired
    private AllinPayTool allinPayTool;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            LogUtil.info(logger, "---------开始处理订单状态----------");
            String msg = new String(message.getBody());
            AllinPayResultNotify allinPayResultNotify = JSON.parseObject(msg, AllinPayResultNotify.class);


            if(allinPayResultNotify !=null)
            {
                allinPayTool.executeNotify(allinPayResultNotify);
            }

            if(allinPayResultNotify.getBIZTYPE()== AllinPayResultNotify.BIZTYPE_SUUCESS &&
                    AllinPayResultCode.PAY_PROCESS_SUCCESS.equals(allinPayResultNotify.getRETCODE()))
            {

            }
            else if(allinPayResultNotify.getBIZTYPE()== AllinPayResultNotify.BIZTYPE_FAIL &&
                    AllinPayResultCode.PAY_PROCESS_SUCCESS.equals(allinPayResultNotify.getRETCODE()))
            {

            }
            else if(allinPayResultNotify.getBIZTYPE()== AllinPayResultNotify.BIZTYPE_REFUND)
            {

            }

            LogUtil.info(logger, "---------处理订单状态成功----------");
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.info(logger, "---------处理订单状态失败，请进行交易明细查询----------");
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
